<template>
  <div class="dialog">
    <el-dialog :title="'区域' + titleText" :visible.sync="showDialog" width="50%"  :close-on-click-modal="false"
      @close="cancel">
      <el-form ref="ruleForm" :model="formData" :rules="rules"  label-width="120px" class="demo-ruleForm">
        <el-form-item label="巡查类型名称："  prop="name">
          <el-input v-model="formData.name" clearable placeholder="请输入巡查类型名称" />
        </el-form-item>
        <el-form-item label="是否启用："  prop="enable">
          <el-select class="w100p" filterable clearable v-model="formData.enable" placeholder="选择区域类型">
            <el-option label="启用" :value='true' />
            <el-option label="停用" :value='false' />
          </el-select>
        </el-form-item>
        <el-form-item label="备注：" class="w100p" prop="remark">
          <el-input type="textarea" v-model="formData.remark" clearable placeholder="请输入" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import PondPatrolArrangementApi from "@/api/basic-info-manage/pondPatrolArrangement";
export default {
  name: '',
  components: {},
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    nowChanceStau: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    editObject: {
      type: [Object],
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      formData: {
        enable: true,
        name: '',
        remark: ''
      },
      areaTypeList: [],
      rules: {
        name: [
          { required: true, message: "请输入区域名称", trigger: ["change", "blur"] }
        ],
        areaType: [
          { required: true, message: "选择区域类型", trigger: ["change", "blur"] }
        ],
        enable: [
          { required: true, message: "请选择是否启用", trigger: ["change", "blur"] }
        ],
      },
      showDialog: this.show

    };
  },

  watch: {
    show(v) {
      this.showDialog = v;
    },
    nowChanceStau(v) {
      if (v === 'add') {
        this.formData = {
          enable: true,
          name: '',
          remark: ''
        }
        this.$nextTick(() => {
          this.$refs['ruleForm'].clearValidate()
        })
      }
      if (v === 'edit') {
        this.formData = this.editObject
      }
    }
  },
  created() {
  },
  mounted() {
  },
  methods: {
    save(formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            PondPatrolArrangementApi.edit(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("修改成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          } else if (this.nowChanceStau === "add") {
            PondPatrolArrangementApi.add(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("添加成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    close() {
      this.$emit("closeDialog");
    },
    cancel() {
      this.$emit("cancelDialog");
    }
  }
};
</script>

<style lang="scss" scoped></style>
